﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Fodee.BusinessComponent;
using Fodee.DataEntity;
using Fodee.Common;

namespace FodeeAdmin.SystemSettings
{
    public partial class UserForm : BaseSystemSettingsForm
    {
        public UserForm()
        {
            InitializeComponent();
            this.grid.AutoGenerateColumns = false;

            BindUserGroup();
        }


        private fodUsersBC bc = new fodUsersBC();
        private fodUserGroupBC userGroupBC = new fodUserGroupBC();

        public void InitForm()
        {
        }

        public void BindData()
        {
            FodeeDS = bc.GetAll();
            this.grid.DataSource = FodeeDS.fod_Users.DefaultView;
            this.grid.ClearSelection();
            ClearForm();
        }

        private void BindUserGroup()
        {
            FodeeDataSet dsUserGroup = userGroupBC.GetAll();
            this.cbxUserGroup.DataSource = dsUserGroup.fod_UserGroup.DefaultView;
            this.cbxUserGroup.DisplayMember = "UserGroupName";
            this.cbxUserGroup.ValueMember = "ID";
        }

        public override void ClearForm()
        {
            base.ClearForm();
            this.grid.ClearSelection();
            SetOperation(Operation.Add);
        }

        public void AddData()
        {
            FodeeDataSet ds = new FodeeDataSet();
            FodeeDataSet.fod_UsersRow row = ds.fod_Users.Newfod_UsersRow();
            row.UserName = this.tbxUserName.Text;
            row.UserGroupID = int.Parse(this.cbxUserGroup.SelectedValue.ToString());
            row.UserGroupName = this.cbxUserGroup.Text;
            row.UserPassword = this.tbxPassword.Text;
            Util.SetCommonFields(row);
            ds.fod_Users.Addfod_UsersRow(row);
            bool result = bc.Insert(ds);
            if (result)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        public void UpdateData()
        {
            FodeeDataSet.fod_UsersRow row = FodeeDS.fod_Users.FindByID(KeyID);
            row.UserName = this.tbxUserName.Text;
            row.UserGroupID = int.Parse(this.cbxUserGroup.SelectedValue.ToString());
            row.UserGroupName = this.cbxUserGroup.Text;
            row.UserPassword = this.tbxPassword.Text;
            bool result = bc.Update(FodeeDS);
            if (result)
            {
                MessageBox.Show("修改成功");
                BindData();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        public void FillData()
        {
            FodeeDataSet.fod_UsersRow row = FodeeDS.fod_Users.FindByID(KeyID);
            this.tbxUserName.Text = row.UserName;
            this.cbxUserGroup.SelectedValue = row.UserGroupID;
            this.tbxPassword.Text = row.UserPassword;
            this.tbxConfirmPassword.Text = row.UserPassword;

            SetOperation(Operation.Edit);

            if (!base.ValidateForm())
            {
                return;
            }
        }

        public void DeleteData()
        {
            bool result = bc.Delete(KeyID);
            if (result)
            {
                MessageBox.Show("删除成功");
                BindData();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private bool ValidatePassword()
        {
            if (this.tbxPassword.Text != this.tbxConfirmPassword.Text)
            {
                MessageBox.Show("密码不一致");
                return false;
            }

            return true;
        }

        public void SetOperation(Operation operation)
        {
            if (operation == Operation.Add)
            {
                this.btnSave.Text = "添加";
            }
            else
            {
                this.btnSave.Text = "修改";
            }

            FormOperation = operation;
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!base.ValidateForm())
            {
                return;
            }

            if (!ValidatePassword())
            {
                return;
            }

            if (FormOperation == Operation.Edit)
            {
                UpdateData();
            }
            else
            {
                AddData();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            KeyID = int.Parse(this.grid.Rows[e.RowIndex].Cells["ID"].Value.ToString());
            string action = this.grid.Columns[e.ColumnIndex].Name;
            if (action.ToLower() == "delete")
            {
                bool isDelete = MessageBox.Show("确定删除吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK;
                if (isDelete)
                {
                    DeleteData();
                }
            }
            else
            {
                FillData();
            }
        }
    }
}
